<template>
  <div class="indexTable bgWhite " id="socialMedia" ref="content">
   <el-form ref="form" class="pb_form">
    <el-input v-model="searchForm.account.value " clearable placeholder="账号" @keyup.enter.native="getTableList"></el-input>
    </el-form>
    <div class="btnContainer" ref="btn">
      <div>
        <el-button type="search" @click="closeAddDialog" v-if="$_has('/social_media/add')">添加</el-button>
        <el-button type="del" @click="sureCloseDialog('del')" v-if="$_has('/social_media/del')">删除</el-button>
      </div>
      <div>
        <el-button type="search" @click="getTableList" v-if="$_has('/social_media/list')">查询</el-button>
        <el-button type="del" @click="resetForm">重置</el-button>
      </div>
    </div>
    <el-table :data="listTable" :height="tableH" :row-class-name="rowClassName" ref="table" @row-click="RowClick"
     @selection-change="handleSelectionChange" :row-key="function(row){return row.id}" v-loading="loading">
      <el-table-column type="selection" width="64"  fixed="left" :reserve-selection="true"></el-table-column>
      <el-table-column  label="图片"  width="208" prop="icon">
        <template slot-scope="scope">
           <el-image  :preview-src-list="scope.row.array_image" :src="utils.setCdnUrl() + scope.row.image"   class="icon_socialmedia"></el-image>
        </template>
      </el-table-column>
      <el-table-column  label="账号"  prop="account"></el-table-column>
      <el-table-column  label="创建人"  prop="create_name"></el-table-column>
      <el-table-column  label="操作" fixed="right"  width="112" class-name="options">
        <template slot-scope="scope" >
          <i class="iconfont  icon-edit_normal"  v-if="$_has('/social_media/edit')" @click.prevent="setCurRow(scope.row, 'edit')"></i>  
          <i class="iconfont  icon-delete_normal" v-if="$_has('/social_media/del')" @click.prevent="setCurRow(scope.row, 'del')" ></i>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination ref="pagination" background :page-sizes="pageSizes" :total="total"  :page-size="pageSize"
    layout="total, sizes, prev, pager, next, jumper"  @size-change="handleSizeChange" :current-page="page"
      @current-change="handleCurrentChange"></el-pagination>
    <Add v-show="addShow" :show="addShow" @closeDialog="closeAddDialog"></Add>
    <Edit v-show="editShow" :id="curId" :show="editShow" @closeDialog="closeEditDialog"></Edit>
    <Del v-show="delShow" :idList="curList" :show="delShow" @closeDialog="closeDelDialog"></Del>
  </div>
</template>

<script>
import { Add, Edit, Del } from './components'
import { list } from '@/api/index/socialMedia.js'
import { setSearchParams } from '@/utils/index.js'

export default {
  name: 'social_media',
  data() {
    return {
      searchForm: {
        account: { value: '', op: 'LIKE' },
      },
      listTable: [  ],
      pageSizes: [20, 50, 100],
      total: 10,
      pageSize: 20,
      page: 1,
      addShow: false,
      editShow: false,
      delShow: false,
      curList: [],
      curId: null,
      tableH: null,
      curIdList: [],
      selectList: [],
      loading: false

    }
  },
  created(){
    this.getTableList()
  },
  mounted(){
    let that = this;
    that.setTableHeight()
    // 注：window.onresize只能在项目内触发1次
    window.onresize = function windowResize () {
        // 通过捕获系统的onresize事件触发我们需要执行的事件
      that.setTableHeight()
    }
  },
  methods: {
    RowClick (row) {
      this.$refs.table.toggleRowSelection(row);
    } ,
    setTableHeight(){
      let searchFormH = this.$refs.form.$el.offsetHeight;
      let manageMenuH = this.$refs.content.offsetHeight;
      let btnContainerH = this.$refs.btn.offsetHeight;
      let paginationH = this.$refs.pagination.$el.offsetHeight
      let tableHeight = manageMenuH  - btnContainerH - searchFormH - paginationH - 48 - 24 - 24
      this.tableH = tableHeight +'px' 
    },
    sureCloseDialog(type){
      if(this.curList.length > 0){
        if(type == 'del'){
          this.closeDelDialog()
        }
      } else {
        let word = "删除"
        this.$message.error('请选择需' + word + '的选项！')
      }
      
    },
    getTableList(){
      this.loading = true
      let params = setSearchParams(this.searchForm, this.pageSize, this.page)
      list(params).then(res=>{
        let list = res.data.data
        if( list.length == 0 && this.page > 1){
          this.page -= 1
          this.getTableList()
        }
        let idList = []
        for(let i = 0; i < list.length; i++){
          idList.push(list[i].id)
          list[i].array_image = [this.utils.setCdnUrl() + list[i].image]
        }
        this.curIdList = idList
        this.listTable = list
        this.total = res.data.total
        this.page = res.data.current_page
        this.$refs.table.clearSelection()
        this.loading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getTableList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getTableList()
      this.$refs.table.clearSelection()
    },
    closeAddDialog(){
      this.addShow = !this.addShow
      if(!this.addShow){
        this.getTableList()
      }
    },
    setCurRow(row, type) {
      this.curList = []
      this.curList.push(row.id)
      this.curId = row.id

      if(type == 'edit'){ 
        this.closeEditDialog()  
      }else if(type == 'del'){
        this.closeDelDialog()
      }
      
    },
    closeEditDialog(){
      this.editShow = !this.editShow
      if(!this.editShow){
        this.getTableList()
      }
    },
    closeDelDialog(){
      this.delShow = !this.delShow
      if(!this.delShow){
        this.getTableList()
      }
    },
    rowClassName({row}){
      let rowClass = "tableRowClass"
      if(row.statusSelection){
        rowClass += ' selectRow'
      }
      return rowClass
    },

    handleSelectionChange(list){
      let array = []
      for(let i = 0; i < list.length; i++ ){
        array.push(list[i].id)
      }
      this.curList = array
      // 选中行的样式赋值
      for(let i in this.selectList){
        this.selectList[i].statusSelection = false
      }
      this.selectList = list
    },
     // 重置按钮
    resetForm(){
      for(let item in this.searchForm){
        this.searchForm[item].value =""
      }
      this.getTableList()
    },
  },
  components: { Add, Edit, Del }

}
</script>

<style lang="scss">

@import '@/styles/index/socialmedia.scss';
</style>